package p2p;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;

import jhttpp2.Jhttpp2HTTPSession;
import jhttpp2.Jhttpp2Read;

public class RequestUrlLocalThread extends Thread {
	
	private String url;
	private Jhttpp2HTTPSession sesson;
	
	public RequestUrlLocalThread(Jhttpp2HTTPSession jhttpsesson, String murl) {
		url = murl;
		sesson = jhttpsesson;
		
		start();
	}

	public void run() {
		progressRequestUrlFromLocal();
    }
	
	private void progressRequestUrlFromLocal() {
		// TODO Auto-generated method stub
		try {
			System.out.println("URL_LOCAL:" + url);
			
			//wait url
			String hashurl = Jhttpp2Read.hashString(url);
			P2pClientThread.urlwait.put(hashurl, 1);
			
			//if not have cache run this code to forward request to friend
			int countInt = 6;
			
			Random generator = new Random();
			Object[] values = P2pClientThread.peerhm.values().toArray();
			ArrayList<Integer> arrInt = new ArrayList<Integer>();
			int loop;
			
			if( countInt > P2pClientThread.peerhm.size() )
				loop = P2pClientThread.peerhm.size();
			else 
				loop = countInt;
			
//			System.out.println("LOOP: " + loop);
			
			// forward request url to peer, with maximum number of peer i can is 6 for friend size
			for ( int i = 0; i < loop; i++ ) {
				int index;
				PeerInfo randomValue;
				//sinh ra mot id khong co
				do {
					index = generator.nextInt(values.length);
				}
				while( arrInt.indexOf(index) != -1);
				
				randomValue = (PeerInfo) values[index];
				if( randomValue.uuid == ControlHandler.myUuid ) 
					continue;
				
				//enable flag of index
				arrInt.add(index);
//				System.out.println(arrInt.indexOf(index));
				
				Socket forwardSock = new Socket(randomValue.ip, Integer.parseInt(randomValue.port));
				PrintWriter os = new PrintWriter(forwardSock.getOutputStream( ), true);
				String mesg = "REQUEST_URL";
				os.print( mesg + "\r\n");
				os.flush();
				os.print( "UUID=" + ControlHandler.myUuid + "\r\n");
				os.flush();
				os.print( "IP=" + Inet4Address.getLocalHost().getHostAddress().toString() + "\r\n");
				os.flush();
				os.print( "PORT=" + P2pClientThread.ECHOPORT + "\r\n");
				os.flush();
				os.print( "URL=" + url + "\r\n");
				os.flush();
				os.print( "COUNT=" + String.valueOf(countInt) +"\r\n");
				os.flush();
				
				String newForwardStr = ControlHandler.myUuid;
				os.print( "FORWARD=" + newForwardStr +"\r\n");
				os.flush();
				
				os.print( "FORWARD_INFO="+ ControlHandler.myUuid + "," + P2pClientThread.ECHOPORT + "\r\n");
				os.flush();
//				forwardSock.shutdownOutput();
				System.out.println("URL_LOCAL:Send friend:" + randomValue.uuid);

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
